package com.ms.p2p.utils;

import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JsonDataGridHelper {
    protected static Logger logger = Logger.getLogger(JsonDataGridHelper.class);
    @SuppressWarnings("unchecked")
    public static PageBean createJSONData(PageBean pager){
        if(pager == null){
            return null;
        }
        List<?> dataList = pager.getList();
        pager.setList(createJSONData(dataList));
        return pager;
    }

    /**
     * 将page类转换成ligerUI Grid框架所需的JSON格式数据。
     * @param data
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List<Object> createJSONData(List dataList){
        Map<String, String> fieldMap = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        HttpServletRequest request = AppContext.getRequest();
        String filedStr = request.getParameter("field");
        if(StringUtils.isEmpty(filedStr)){
            return null;
        }
        String[] field = filedStr.split(",");
        String[] dict = StringUtils.splitPreserveAllTokens(request.getParameter("dict"), ",");
        String[] i18n = StringUtils.splitPreserveAllTokens(request.getParameter("i18n"), ",");
        String[] isdate = StringUtils.splitPreserveAllTokens(request.getParameter("isdate"), ",");

        for(Object data: dataList){
            Map<String,String> keyValue = new HashMap<String,String>();
            for(int i = 0; i < field.length;i++){
                String value = getValueByOgnl(data, field[i],"");
                if (i18n != null && i18n[i].trim().length()>0 && !StringUtils.isBlank(value)){
                    String fieldName = null;
                    if(fieldMap.containsKey(field[i])){
                        fieldName = fieldMap.get(field[i]);
                    }else{
                        fieldName = StringUtils.capitalize(i18n[i]);
                    }
                    String propertiesValue = SpringUtils.
                            getMessage(fieldName + "." + value, null);
                    keyValue.put(field[i], propertiesValue);
                }else if (dict != null && dict[i].trim().length()>0 && !StringUtils.isBlank(value)){
                    String propertiesValue =  DictionaryUtils.getValue(dict[i].trim(),value);
                    keyValue.put(field[i], propertiesValue);
                }else if(isdate!=null&&isdate[i].trim().length()>0&&!StringUtils.isBlank(value)){
                    String propertiesValue = MsDateUtils.TimeStamp2DateTime(value);
                    keyValue.put(field[i], propertiesValue);

                }
                else{
                    keyValue.put(field[i], value);
                }
            }
            list.add(keyValue);
        }
        return list;
    }

//    /**
//     * 根据数据源中所列的字段获取值,为NULL时 需要设置默认值(待完善)
//     * @param data
//     * @return
//     * @throws OgnlException
//     */
    private static String getValueByOgnl(Object target,String field,String defaultValue) {
        String resultValue = "";

        Object value = null;
        try {
            value = BeanUtils.getProperty(target, field);
        } catch (Exception e) {

            logger.error("系统错误",e);


            return defaultValue;
        }
        if(null != value){
                resultValue = String.valueOf(value);

        }
        return resultValue;
    }
}
